package com.xbb.controller.sys;

import com.xbb.common.RespResult;
import com.xbb.entity.Questionnaire;
import com.xbb.entity.User;
import com.xbb.entity.UserQuestionnaire;
import com.xbb.mapper.QuestionnaireMapper;
import com.xbb.mapper.UserMapper;
import com.xbb.mapper.UserQuestionnaireMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("/sys/userQuestionnaire")
public class SysUserQuestionnaireController {

    private String prefix = "/sys/questionnaire/";

    @Resource
    private UserQuestionnaireMapper userQuestionnaireMapper;
    @Resource
    private QuestionnaireMapper questionnaireMapper;
    @Resource
    private UserMapper userMapper;

    // 问卷调查结果

    // 分页数据
    @ResponseBody
    @RequestMapping("/data")
    public RespResult data (@RequestParam(value = "title", required = false, defaultValue = "") String title,
                            @RequestParam(value = "name", required = false, defaultValue = "") String name,
                            @RequestParam(value = "cellphone", required = false, defaultValue = "") String cellphone,
                            @RequestParam(value = "questionnaireId", required = false) Integer questionnaireId) {
        if (title.equals("")) title = null;
        if (name.equals("")) name = null;
        if (cellphone.equals("")) cellphone = null;
        List<UserQuestionnaire> data = userQuestionnaireMapper.selectListByPaging(
                title,
                name,
                cellphone,
                questionnaireId,
                null
        );
        int count = userQuestionnaireMapper.selectCountByPaging(
                title,
                name,
                cellphone,
                questionnaireId,
                null
        );

        Set<UserQuestionnaire> userQuestionnaireSet = new HashSet<>();
        for (UserQuestionnaire userQuestionnaire : data) {
            userQuestionnaireSet.add(userQuestionnaire);
        }

        RespResult respResult = new RespResult();
        respResult.success(userQuestionnaireSet, count);
        return respResult;
    }

    // 调查结果详情
    @RequestMapping("/result.html")
    public String resultHtml (@RequestParam("id") int id, Model model) {
        UserQuestionnaire userQuestionnaire = userQuestionnaireMapper.selectByPrimaryKey(id);
        Questionnaire questionnaire = questionnaireMapper.selectByPrimaryKey(userQuestionnaire.getQuestionnaireId());
        User user = userMapper.selectByPrimaryKey(userQuestionnaire.getUserId());
        List<UserQuestionnaire> userQuestionnaireList = userQuestionnaireMapper.selctListByQuestionnaireId(
                userQuestionnaire.getQuestionnaireId(), userQuestionnaire.getUserId()
        );
        model.addAttribute("questionnaire", questionnaire);
        model.addAttribute("u", user);
        model.addAttribute("userQuestionnaireList", userQuestionnaireList);
        return prefix + "result";
    }

    // 删除
    @ResponseBody
    @RequestMapping("/delete")
    public RespResult delete(@RequestBody Integer id) {
        userQuestionnaireMapper.deleteByPrimaryKey(id);
        return new RespResult();
    }

}
